/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.bizon.dinner.gui.topcompoments;

import java.util.ArrayList;
import java.util.List;

import javax.swing.table.AbstractTableModel;

import org.bizon.dinner.gui.dialogs.SecuritySystem;

import diiner.common.webaccess.ReportLine;
import diiner.common.webaccess.UnAutorizationException_Exception;
import java.util.Set;
import java.util.TreeSet;

/**
 * 
 * @author Bizon
 */
public class ResultsTableModel extends AbstractTableModel {

	private static final long serialVersionUID = 4309828867585050609L;
	private List<ReportLine> results;
	private List<String> users;

	String[] columnNames = new String[] { "eat name", "count", "price" };

	public ResultsTableModel() {
		init();
	}

	@Override
	public Class<?> getColumnClass(int columnIndex) {
		return columnIndex > 2 ? Boolean.class : Object.class;
	}

	public int getColumnCount() {
		return 3 + users.size();
	}

	@Override
	public String getColumnName(int column) {
		return column >= 3 ? users.get(column - 3) : columnNames[column];
	}

	public int getRowCount() {
		return results.size();
	}

	public Object getValueAt(int rowIndex, int columnIndex) {
		ReportLine line = results.get(rowIndex);
		switch (columnIndex) {
		case 0:
			return line.getName();
		case 1:
			return line.getCount();
		case 2:
			return line.getPrice();
		default:
			return Boolean.valueOf(line.getUserList().contains(users.get(columnIndex - 3)));
		}
	}

	private void init() {
		try {
			results = SecuritySystem.getDinnerService().getReport(SecuritySystem.getID());
			Set<String> tmpusers = new TreeSet<String>();
			for (ReportLine i : results) {
				tmpusers.addAll(i.getUserList());
			}
                        users = new ArrayList<String>(tmpusers);
		} catch (UnAutorizationException_Exception e) {
			SecuritySystem.showError(e);
		}
	}
}
